﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Configuration
{
    public class NetworkConfiguration : ConfigurationSection
    {
        private static NetworkConfiguration instance = null;
        private static readonly object mylock = new object();

        private NetworkConfiguration() { }

        public static NetworkConfiguration GetInstance()
        {
            lock ((mylock))
            {
                if (instance == null)
                {
                    ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
                    configFileMap.ExeConfigFilename = "Configuration/Game.config";

                    instance = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
                                    .GetSection("NetworkConfiguration") as NetworkConfiguration;
                }
                return instance;
            }
        }

        [ConfigurationProperty("MultiConfigurationsList")]
        public MultiConfigurationsList MultiConfigurationsList
        {
            get
            {
                return base["MultiConfigurationsList"] as MultiConfigurationsList;
            }
        }
    }

    [ConfigurationCollection(typeof(MultiConfiguration), AddItemName = "MultiConfiguration", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class MultiConfigurationsList : ConfigurationElementCollection
    {
        private List<MultiConfiguration> _MultiConfiguration;
        public List<MultiConfiguration> MultiConfiguration
        {
            get
            {
                if (_MultiConfiguration == null)
                {
                    _MultiConfiguration = new List<MultiConfiguration>();
                    foreach (MultiConfiguration config in this)
                    {
                        _MultiConfiguration.Add(config);
                    }
                }
                return _MultiConfiguration;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new MultiConfiguration();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return element as MultiConfiguration;
        }
    }

    public class MultiConfiguration : ConfigurationElement
    {
        [ConfigurationProperty("ID", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string ID
        {
            get
            {
                return base["ID"] as string;
            }
            set
            {
                base["ID"] = value;
            }
        }

        [ConfigurationProperty("Type", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string Type
        {
            get
            {
                return base["Type"] as string;
            }
            set
            {
                base["Type"] = value;
            }
        }

        [ConfigurationProperty("LocalIP", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string LocalIP
        {
            get
            {
                return base["LocalIP"] as string;
            }
            set
            {
                base["LocalIP"] = value;
            }
        }

        [ConfigurationProperty("Dns", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string Dns
        {
            get
            {
                return base["Dns"] as string;
            }
            set
            {
                base["Dns"] = value;
            }
        }

        [ConfigurationProperty("Port", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string Port
        {
            get
            {
                return base["Port"] as string;
            }
            set
            {
                base["Port"] = value;
            }
        }

    }
}
